//
//  WMCustomNavigationBar.swift
//  deWaterMarkingProject
//
//  Created by admin on 2025/9/15.
//

import UIKit


class WMCustomNavigationBar: UIView {
    
    enum Style {
        case opaque   // 不透明（默认）
        case transparent // 完全透明
        case translucent // 半透明（毛玻璃效果）
    }
    
    
    // MARK: - 组件
    private(set) var titleLabel: UILabel!
    private(set) var leftButton: UIButton?
    private(set) var rightButton: UIButton?
    private var bottomLine: UIView!
    private var blurView: UIVisualEffectView?
    
    // MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    // MARK: - UI设置
    private func setupUI() {
        backgroundColor = .clear
        
        // 标题
        titleLabel = UILabel()
        titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
        titleLabel.textColor = .black // 默认白色文字，适合透明背景
        titleLabel.textAlignment = .center
        addSubview(titleLabel)
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
            titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
            titleLabel.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, multiplier: 0.6)
        ])
        
        // 底部细线（默认隐藏）
        bottomLine = UIView()
        bottomLine.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        bottomLine.isHidden = true
        addSubview(bottomLine)
        
        bottomLine.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            bottomLine.bottomAnchor.constraint(equalTo: bottomAnchor),
            bottomLine.leadingAnchor.constraint(equalTo: leadingAnchor),
            bottomLine.trailingAnchor.constraint(equalTo: trailingAnchor),
            bottomLine.heightAnchor.constraint(equalToConstant: 0.5)
        ])
    }
    
    // MARK: - 公共方法
    
    /// 设置导航栏样式
    func setStyle(_ style: Style) {
        switch style {
        case .opaque:
            backgroundColor = .white
            titleLabel.textColor = .black
            bottomLine.isHidden = false
            blurView?.removeFromSuperview()
            blurView = nil
            
        case .transparent:
            backgroundColor = .clear
            titleLabel.textColor = .black
            bottomLine.isHidden = true
            blurView?.removeFromSuperview()
            blurView = nil
            
        case .translucent:
            backgroundColor = .clear
            titleLabel.textColor = .white
            bottomLine.isHidden = true
            
            // 添加毛玻璃效果
            let blurEffect = UIBlurEffect(style: .dark)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            blurEffectView.frame = bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            insertSubview(blurEffectView, at: 0)
            blurView = blurEffectView
        }
    }
    
    /// 设置导航栏标题
    func setTitle(_ title: String) {
        titleLabel.text = title
    }
    
    /// 添加左侧按钮
    func addLeftButton(image: UIImage? = nil, title: String? = nil, tintColor: UIColor = .white, target: Any?, action: Selector) {
        leftButton?.removeFromSuperview()
        
        let button = UIButton(type: .custom)
        if let image = image {
            button.setImage(image, for: .normal)
        }
        if let title = title {
            button.setTitle(title, for: .normal)
        }
//        button.tintColor = tintColor
        button.addTarget(target, action: action, for: .touchUpInside)
        addSubview(button)
        
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
            button.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
            button.widthAnchor.constraint(greaterThanOrEqualToConstant: 24),
            button.heightAnchor.constraint(equalToConstant: 24)
        ])
        
        leftButton = button
    }
    
    /// 添加右侧按钮
    func addRightButton(image: UIImage? = nil, title: String? = nil, tintColor: UIColor = .white, target: Any?, action: Selector) {
        rightButton?.removeFromSuperview()
        
        let button = UIButton(type: .system)
        if let image = image {
            button.setImage(image, for: .normal)
        }
        if let title = title {
            button.setTitle(title, for: .normal)
        }
        button.tintColor = tintColor
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.addTarget(target, action: action, for: .touchUpInside)
        addSubview(button)
        
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
            button.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12),
            button.heightAnchor.constraint(equalToConstant: 24)
        ])
        
        rightButton = button
    }
}
